package com.bing.service.impl;



import com.bing.entity.Bp.AppMessageInfo;
import com.bing.entity.Bp.BpApplyRecordInfo;
import com.bing.entity.Bp.BpInfo;
import com.bing.entity.Bp.CommentsFeedbackinfo;
import com.bing.entity.ImageInfo;
import com.bing.mapper.SendMsgMapper;
import com.bing.mapper.UserMapper;
import com.bing.service.UserService;

import com.bing.utils.idgenertor.IdGen;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 胡日亮
 * @create 2017-12-20 19:01
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    SendMsgMapper sendMsgMapper;
    @Value("${sms.check}")
    private String  check;

    private final static String ID = "1000000001";
    private Integer PAGE_SIZE = 15;

    /**
     * @Description 查询用户名是否存在
     * @Author 胡日亮
     * @Date 2017/12/20 19:39
     */
    @Override
    public int findUser(String user) {
        return userMapper.findUser(user);
    }

    /**
     * @Description 登录验证
     * @Author 胡日亮
     * @Date 2017/12/20 19:39
     */
    @Override
    public int validate(String user, String password) {
        return userMapper.validate(user, password);
    }

    @Override
    public BpInfo  initBpinfo(BpInfo bpInfo) throws Exception {
        if (!check.equals(bpInfo.getVerificaCode())){
            String code =  sendMsgMapper.CheckVerificaCode(bpInfo.getBpTel());
            if (!bpInfo.getVerificaCode().equals(code)){
                throw new Exception("验证码输入有误");
            }
        }

        BpInfo bpInfo1b= userMapper.checkBp(bpInfo.getBpTel());
        if (bpInfo1b == null){
            String bpId = userMapper.getMaxBpId();
            if (StringUtils.isEmpty(bpId)){
                bpInfo.setBpId(ID);
            } else {
                bpInfo.setBpId(String.valueOf(Integer.valueOf(bpId)+1));
            }
            bpInfo.setCreateTime(new Date());
            userMapper.initBpinfo(bpInfo);
            return bpInfo;
        } else {
            //标识登陆
            userMapper.setSign(bpInfo1b);
            return bpInfo1b;
        }

    }

    /**
     * 用户列表
     * @return
     */
    @Override
    public PageInfo<BpInfo> getBpinfoList(Integer pageNum) {
        PageHelper.startPage(pageNum, PAGE_SIZE);
        List<BpInfo> bpinfoList = userMapper.getBpinfoList();
        return new PageInfo<BpInfo>(bpinfoList);
    }

    @Override
    public BpInfo checkBp(String tel) {
        return userMapper.checkBp(tel);
    }

    /**
     * 意见反馈
     * @param commentsFeedbackinfo
     */
    @Override
    public void initCommet(CommentsFeedbackinfo commentsFeedbackinfo) {
        commentsFeedbackinfo.setComId(IdGen.uuid());
        commentsFeedbackinfo.setCreateTime(new Date());
        userMapper.initCommet(commentsFeedbackinfo);
    }

    public List<AppMessageInfo> getAppMessageInfoList(){
        return userMapper.getAppMessageInfoList();
    }

    /**
     * 申请记录
     * @param bpId
     * @return
     */
    @Override
    public List<BpApplyRecordInfo> getBpregList(String bpId) {
        return userMapper.getBpregList(bpId);
    }

    /**
     * 图片路径
     * @param imageInfo
     */
    @Override
    public void addImageInfo(ImageInfo imageInfo) {
        userMapper.addImageInfo(imageInfo);
    }

}
